package com.maker.mcp.client.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Slf4j
@Aspect
public class ServiceLogAspcet {


    @Around("execution(* com.maker.mcp.client.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
//        long start = System.currentTimeMillis();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object proceed = joinPoint.proceed();
        String point = joinPoint.getTarget().getClass().getName()+"."+joinPoint.getSignature().getName();
//        long end = System.currentTimeMillis();
        stopWatch.stop();
//        long takeTime = end - start;
        long takeTime = stopWatch.getTotalTimeMillis();
        if(takeTime>5000){
            log.error("{} 耗时偏长{}毫秒",point,takeTime);
        }else if(takeTime>2000){
            log.warn("{} 耗时中等{}毫秒",point,takeTime);
        }else{
            log.info("{} 耗时{}毫秒",point,takeTime);
        }
        return proceed;
    }
}
